<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>

<%@include file="head.jsp"%><br/>
<br/><br/>

<form action="queryServletFrontDesk" method="get">
    --------------- 图书列表 ---------------
    <select name="type" id="type">
        <c:forEach items="${types}" var="type">
            <c:if test="${sessionScope.typeSelected == type}">
                <option value="${type}" selected>
                    <c:out value="${type}"/>
                </option>
            </c:if>
            <c:if test="${sessionScope.typeSelected != type}">
                <option value="${type}">
                    <c:out value="${type}"/>
                </option>
            </c:if>
        </c:forEach>
    </select>
    <input name="keyword" placeholder="输入书名" value="${sessionScope.queryStr}">
    <button>搜索</button>
</form>

<table>
    <c:forEach items="${books}" var="book">
        <tr>
            <c:if test="${!sessionScope.cartList.toString().contains(book.getName())}">
                <td><button onclick="addBookIntoCart(${book.getId()})">加入购物车</button></td>
            </c:if>
            <c:if test="${sessionScope.cartList.toString().contains(book.getName())}">
                <td><button disabled>加入购物车</button></td>
            </c:if>
            <td><c:out value="序列号:${book.getId()}"/></td>
            <td><c:out value="书名:《${book.getName()}》"/></td>
            <td><c:out value="作者:${book.getAuthor()}"/></td>
            <td>&nbsp;<c:out value="类型:${book.getType()}"/></td>
            <td>&nbsp;<c:out value="价格:${book.getPrice()}"/></td>
            <td>￥</td>
            <td>&nbsp;<c:out value="库存:${book.getInventory()}"/></td>
            <td><a onclick="detailsShowFromDesk(${book.getId()})">详情</a></td>
            <c:if test="${sessionScope.detailsShowMap.get(book.getOne()) == book.getId()}">
                <td rowspan="4">
                    <div class="details-show">
                        <c:out value="${book.getDetails()}"/>
                    </div>
                </td>
            </c:if>
        </tr>
    </c:forEach>
    <c:if test="${books.toString() != '[]'}">
        <tr><td>&nbsp;</td></tr>
        <tr><td>&nbsp;</td></tr>
        <tr><td>&nbsp;</td></tr>
    </c:if>
</table>
<c:if test="${books.toString() == '[]'}">
    （竟然什么也搜不到=。=）
    <br/><br/>
</c:if>

</div>

------------ 购物车（订单）
<c:if test="${sessionScope.user == null}">
    （请登录）
</c:if> ------------
<br/><br/>
<c:if test="${sessionScope.user != null}">
    <c:set var="total" value="${0}"/>
    <table>
        <c:forEach items="${booksAll}" var="book">
            <c:if test="${sessionScope.cartList.toString().contains(book.getName())}">
                <tr>
                    <td><button onclick="deletedBookFromCart(${book.getId()})">删除</button></td>
                    <td><c:set var="total" value="${total + book.getPrice() * book.getCurrentNum()}"/></td>
                    <td><c:out value="序列号:${book.getId()}"/></td>
                    <td><c:out value="书名:《${book.getName()}》"/></td>
                    <td><c:out value="作者:${book.getAuthor()}"/></td>
                    <td><c:out value="类型:${book.getType()}"/></td>
                    <td><c:out value="价格:${book.getPrice()}￥"/></td>
                    <td><c:out value="数目:${book.getCurrentNum()}本"/></td>
                    <c:if test="${book.getInventory() > 0}">
                        <td><button onclick="updateBookNumFromCart(${book.getId()}, ${1})">+</button></td>
                    </c:if>
                    <c:if test="${book.getInventory() == 0}">
                        <td><button disabled>+</button></td>
                    </c:if>
                    <c:if test="${book.getCurrentNum() > 1}">
                        <td><button onclick="updateBookNumFromCart(${book.getId()}, ${-1})">-</button></td>
                    </c:if>
                    <c:if test="${book.getCurrentNum() == 1}">
                        <td><button disabled>-</button></td>
                    </c:if>
                </tr>
            </c:if>
        </c:forEach>
    </table>
    <br/>（购物车到底啦）
</c:if>

<c:if test="${total.toString().length() > 6}">
    <c:out value=" 总计:${total.toString().substring(0, 5)}￥"/>
</c:if>
<c:if test="${total.toString().length() <= 6}">
    <c:out value=" 总计:${total}￥"/>
</c:if>

</body>
</html>

<style>
    .details-show{
        width: 300px;
        height: 100px;
        border: 1px gray solid;
    }
    .line0{
        width: 580px;
        border: 1px gray solid;
        border-bottom: none;
        margin-bottom: 10px;
    }
</style>

<script>
    function addBookIntoCart(id) {
        window.location.href = "http://localhost:8080/JavaBookShop/addBookIntoCartServlet?id="+id;
    }
    function deletedBookFromCart(id) {
        window.location.href = "http://localhost:8080/JavaBookShop/deletedBookFromCartServlet?id="+id;
    }
    function updateBookNumFromCart(id, num) {
        window.location.href = "http://localhost:8080/JavaBookShop/updateBookNumFromCartServlet?id="+id+"&num="+num;
    }
    function detailsShowFromDesk (id) {
        window.location.href = "http://localhost:8080/JavaBookShop/detailsShowFromDeskServlet?id="+id;
    }
</script>

